<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>楼座单元维护</title>
  </head>
  <body>
    <script type="text/javascript">
      var newBuildingUnitWin;
      var editBuildingUnitWin;
      var buildingUnitDS;
      Ext.onReady(function(){
        var BuildingId;
        Ext.QuickTips.init();
        Ext.BLANK_IMAGE_URL = '/javascripts/ext/resources/images/default/s.gif';
        //------------------------------业主Grid--------------------------------
        buildingUnitDS = new Ext.data.Store({
          paramNames: {
            start: "start",
            limit: "limit"
          },
          baseParams: {
            start: 0,
            limit: pageSize + 15
          },
          proxy: new Ext.data.HttpProxy({
            url: '/building/getAllBuildingUnits'
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'number'
            },{
              name: 'name'
            },{
              name: 'sub_type'
            },{
              name: 'is_locked'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }])
        });
        //house的CM
        var BuidlingUnitCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),{
            id: 'common',
            header: '数据库编号',
            dataIndex: 'id',
            width: 30,
            hidden:true
          },{
            header: '编号',
            dataIndex: 'number',
            width: 100
          },{
            header: '楼座单元名',
            dataIndex: 'name',
            width: 170
          },{
            header: '建筑类型',
            dataIndex: 'sub_type',
            width: 80
          },{
            header: '是否锁定',
            dataIndex: 'is_locked',
            renderer: renderBuildingUnitIsLucked,
            width: 80
          },{
            header: '生成日期',
            dataIndex: 'created_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 110
          },{
            header: '修改日期',
            dataIndex: 'updated_at',
            renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
            width: 110
          }
        ]);
        //render 楼座是否已经锁定
        function renderBuildingUnitIsLucked(value, cellmeta, record, rowIndex, columnIndex, store){
          if (record.get("is_locked")=="1"){
            return "<font color=red>锁定<font>";
          }else{
            return "<font color=green>未锁定<font>";
          }
        }
        
        //建筑单元的Grid

        //top toolbar
        var menubar = [{
            text: '添加新楼座单元',
            iconCls: 'addItem',
            handler: rightMenuNewBuildingUnitFn
          }];

        var BuidlingUnitGrid = new Ext.grid.GridPanel({
          //viewConfig : {
          //  forceFit: true
          //},
          store: buildingUnitDS,
          cm: BuidlingUnitCM,
          renderTo: 'building_unit_search',
          //width: 830,
          height: screenHeight - 260,
          //frame: true,
          collapsible: true,
          autoExpandColumn: 'common',
          title: '楼座列表',
          frame: false,
          loadMask: true,
          tbar: menubar,
          bbar: new Ext.PagingToolbar({
            pageSize: pageSize + 15,
            store: buildingUnitDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });

        BuidlingUnitGrid.on('rowcontextmenu', function(housegrid, rowIndex, e){
          BuildingId = buildingUnitDS.getAt(rowIndex).get('id');
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        
        //############ 订单-右键菜单 ##########
        var addBuildingUnitMenuItem = new Ext.menu.Item({
          iconCls: 'addItem',
          text: '【添加新楼座单元】',
          //iconCls: 'album',
          handler: rightMenuNewBuildingUnitFn
        });
        var editBuildingUnitMenuItem = new Ext.menu.Item({
          iconCls: 'editItem',
          text: '【编辑楼座单元】',
          //iconCls: 'album',
          handler: rightMenuEditBuildingUnitFn
        });
        
        var buildingUnitLockMenuItem = new Ext.menu.Item({
          text: '【锁定与解锁】',
          //iconCls: 'album',
          handler: rightMenuLockBuildingUnitFn
        });

        var rightClick = new Ext.menu.Menu({
          items: [addBuildingUnitMenuItem,editBuildingUnitMenuItem,buildingUnitLockMenuItem]
        });
        
        function rightMenuNewBuildingUnitFn(grid, rowIndex, e){
          newBuildingUnitWin = new Ext.Window({
            layout: 'fit',
            width: 275,
            height: 160,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            title: '添加新楼座单元',
            autoLoad: {
              url: "/building/newBuildingUnit",
              scripts: true
            }
          });
          newBuildingUnitWin.resizable = false;
          newBuildingUnitWin.show();
        }
        function rightMenuEditBuildingUnitFn(grid, rowIndex, e){
          editBuildingUnitWin = new Ext.Window({
            layout: 'fit',
            width: 285,
            height: 160,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            title: '编辑楼座单元',
            autoLoad: {
              url: "/building/editBuildingUnit?building_id="+BuildingId,
              scripts: true
            }
          });
          editBuildingUnitWin.resizable = false;
          editBuildingUnitWin.show();
        }
        function rightMenuLockBuildingUnitFn(grid, rowIndex, e){
          Ext.Ajax.request({
            url : '/building/buildingUnitLock',
            success : function (result, request) {
              buildingUnitDS.reload();
            },
            failure : function (result, request){
              Ext.MessageBox.alert("提示","通讯失败，请从新操作");
            },
            method : 'POST',
            params : {
              building_id : BuildingId,
              authenticity_token: authToken.getValue()
            }
          });
        }
        
        buildingUnitDS.load();
      })
    </script>
    <div id="building_unit_search"></div>
  </body>
</html>
